using System;
using System.Collections.Generic;
using System.Linq;

namespace ArmyManager.Commands
{
	public class CommandList : ICommand
	{
		private readonly List<ICommand> _commands;

		public CommandList(IEnumerable<ICommand> commands = null)
		{
			_commands = new List<ICommand>(commands ?? new ICommand[0]);
		}

		public void Execute()
		{
			foreach (ICommand command in _commands)
			{
				command.Execute();
			}
		}

		public void Undo()
		{
			foreach (ICommand command in ((IEnumerable<ICommand>)_commands).Reverse())
			{
				command.Undo();
			}
		}

		public void Add(ICommand command)
		{
			_commands.Add(command);
		}

		public int Count { get { return _commands.Count; } }
	}
}